<template>
  <div style="background-color: #000;">
    <div class="loading-wrapper" v-show="showLoading">
      <Loading></Loading>
    </div>
    <div class="container" v-show="!showLoading">
      <!-- 面包屑start -->
      <el-breadcrumb separator="/" class="breadCrumb_Box">
        <el-breadcrumb-item>
          <router-link :to="{path:'mealPay',query:{type:type,code:getCode}}">购买</router-link>
        </el-breadcrumb-item>
        <el-breadcrumb-item>{{c_breadCrumb_name}}</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 面包屑end -->
      <el-row>
        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
          <div class="content">
            <div class="pay flex flex-column justify-center align-center">
              <div :class="code==1?'text1':'text0'">
                当前选择套餐：<span>{{
                  payInfo.payTitle
                }}</span>
              </div>
              <div :class="code==1?'text1':'text0'">
                当前购买数量：<span>{{ payInfo.num }}</span>
              </div>
              <div :class="code==1?'text1':'text0'"
                style="display: flex; align-items: center; flex-direction: row;margin-bottom: 20px;">
                当前支付方式：
                <div>
                  <el-image src="../../../static/price/QianMinTaoCan_WeChat.png"></el-image>
                </div>
                <div style="margin-top: 6px">
                  微信支付
                </div>
              </div>
              <div>
                <div class="erweim" style="text-align: center;">
                  <!-- 未支付 -->
                  <canvas id="canvas"></canvas>
                  <!-- 已支付 -->
                  <img src="../../../static//price/Payment_success_status.png" alt=""
                    style="width:148px;position: absolute;margin-left: -149px;" v-if="code==1">
                  <!-- 过期 -->
                  <img src="../../../static//price/Payment_error_status.png" alt=""
                    style="width:148px;position: absolute;margin-left: -149px;" v-if="statusOrder==-1">
                  <p style="margin-left: -9px;" v-if="code==0">扫一扫，立即支付</p>
                  <p style="margin-left: -9px;" v-if="code==0">
                    总支付:
                    <span class="price">{{ payInfo.allPrice }}</span> 元
                  </p>
                </div>
                <!-- 未支付 -->
                <div style="color:red;font-size: 12px;" v-if="code==0 && statusOrder==0"> 温馨提示:请您尽快支付，二维码的有效时间还剩余
                  <span>{{minute}}分{{second}}秒</span>
                </div>
                <!-- 已支付 -->
                <div style="color:red;font-size: 12px;text-align: center;" v-if="code==1"> 恭喜您支付成功，您可以自主选择需要跳转的界面^-^
                </div>
                <!-- 过期 -->
                <div style="color:red;font-size: 12px;text-align: center;" v-if="statusOrder==-1"> 支付二维码已过期，别再支付了!
                </div>
                <!-- 已支付 -->
                <div style="margin-top: 20px;" class="flex flex-row justify-center align-center">
                  <el-button type="primary" @click="goUrl('/appSuper','appSuper')" v-if="code==1&&getCode==601">
                    返回超级签名的应用列表</el-button>
                  <el-button type="primary" @click="goUrlVisa('/visaRecord','visaRecord')" v-if="code==1&&getCode==700">
                    返回查看封装记录</el-button>
                  <el-button type="primary" @click="goUrlOrder('/orderlist','orderlist')" v-if="code==1">查看我的订单
                  </el-button>
                  <el-button type="primary" v-if="(code==1&&getCode==800)||(code==1&&getCode==801)">暂时不生成</el-button>
                  <el-button type="primary" v-if="(code==1&&getCode==800)||(code==1&&getCode==801)">立即生成安装包</el-button>
                </div>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
  import Loading from '@/components/common/laoding'
  import filters from "../../utils/filters";
  import QRCode from "qrcode";
  export default {
    filters: filters,
    //import引入的组件需要注入到对象中才能使用
    components: {
      QRCode: QRCode,
      Loading
    },
    data() {
      //这里存放数据
      return {
        showLoading: true, // 显示loading
        payUrl: {}, //接收二维码地址
        codes: "",
        payTypes: 0,
        payInfo: [], //数据
        minute: '', //分
        second: '', //秒
        timers: null, // 定时器名称  
        remainTime: '', //总秒
        code: '', //订单状态 0-未支付 1已支付
        statusOrder: '', //0-未过期 -1-已过期
        type: '',
        getCode: '',
        // appID: '',
        c_breadCrumb_name: '微信支付',
        title: ''

      };
    },
    //监听属性 类似于data概念
    computed: {

    },
    //监控data中的数据变化
    watch: {
      second: {
        handler(newVal) {
          this.num(newVal)
        }
      },
      minute: {
        handler(newVal) {
          this.num(newVal)
        }
      }

    },
    //方法集合
    methods: {
      // 路由跳转
      goUrl(path, name) {
        this.$router.push({
          path: path,
          name: name,
        });
      },
      goUrlVisa(path, name) {
        sessionStorage.setItem("setMenuIndex_appPackaging", 1);
        this.$router.push({
          path: path,
          name: name,
        });
        location.reload();
      },
      goUrlOrder(path, name) {
        sessionStorage.setItem("acount_detailInfo", 1);
        this.$router.push({
          path: path,
          name: name,
        });
        location.reload();
      },

      //微信支付信息
      wechatPay() {
        var params = new URLSearchParams();
        params.append("orderid", this.$route.query.orderID);
        // console.log();
        this.$post(this.API.Price_showWxPayPage, params).then((res) => {
          this.showLoading = false
          console.log(res, "zhifu");
          let data = res.data;
          this.payInfo = data;
          this.statusOrder = data.status;
          this.remainTime = data.lastTime; //总秒数
          this.minute = parseInt(data.lastTime / 60 % 60); //分
          this.second = parseInt(data.lastTime % 60); //秒
          var canvas = document.getElementById("canvas"); //二维码
          QRCode.toCanvas(canvas, this.payInfo.wxPayUrl, function (error) {
            if (error) console.error(error);
            // console.log("success!");
          });
        });
      },
      //查询订单信息
      selectOrder() {
        var params = new URLSearchParams();
        params.append("orderID", this.$route.query.orderID);
        this.$post(this.API.Price_checkOrder, params).then((res) => {
          this.code = res.data;
        });
      },

      num: function (n) {
        return n < 10 ? '0' + n : '' + n
      },
      // 倒计时
      add() {
        var _this = this
        var time = window.setInterval(function () {
          if (_this.second === 0 && _this.minute !== 0) {
            _this.second = 59
            _this.minute -= 1
          } else if (_this.minute === 0 && _this.second === 0) {
            _this.second = 0
            window.clearInterval(time)
          } else {
            _this.second -= 1
          }
        }, 1000)
      },

    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created() {
      this.payTypes = this.$route.query.payType;
      this.getCode = this.$route.query.code; //code初始值
      console.error(this.getCode);
      this.type = this.$route.query.type;
      this.wechatPay();
      //微信支付需要前端做2秒每次的轮训，每2秒查询一下订单的状态。
      this.timers = window.setInterval(() => {
        setTimeout(this.selectOrder(), 0);
      }, 2000);

    },
    beforeDestroy() {
      //离开路由销毁定时器
      clearInterval(this.timers);
      this.timers = null;

    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {
      this.add();
    },
  };

</script>
<style scoped>
  /* 面包屑  */
  .breadCrumb_Box {
    margin-top: 30px;
    margin-bottom: 20px;
    color: #ffffff;
  }

  #canvas {
    margin-left: -10px;
    border-radius: 4px;
  }

  .breadcrumb {
    margin: 20px 0px 0px 0px;
    background-color: transparent;
  }

  .content {
    background: #0f0f0f;
    box-shadow: 0px 4px 10px 2px rgba(0, 0, 0, 0.08);
    border-radius: 4px;
    margin-bottom: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .pay {
    margin-top: 30px;
    margin-bottom: 30px;

  }

  .text1 {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #fff;
    line-height: 16px;
    margin-top: 30px;
    text-align: left;
    /* margin-left: 65px; */
  }

  .text0 {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #fff;
    line-height: 16px;
    margin-top: 30px;
    text-align: left;
    /* margin-left: 95px; */
  }

  .text span {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #ff8442;
    line-height: 16px;
  }

  .erweim p {
    color: #ffffff;
  }

  .price {
    font-size: 26px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #F65D26;
    line-height: 20px;
  }

  .el-button {
    border: none;
  }

  .breadCrumb_Box>>>.el-breadcrumb__inner a {
    color: #ee7829;
  }

  .breadCrumb_Box>>>.el-breadcrumb__item .el-breadcrumb__separator {
    color: #fff;
  }

  .breadCrumb_Box>>>.el-breadcrumb__item .el-breadcrumb__inner {
    color: #fff;
  }

</style>
